package com.wei.web.demo.util.limiter;

import com.wei.web.demo.util.limiter.repository.MemoryLimiterRepository;

import java.util.concurrent.TimeUnit;

/**
 * @author buhuan.wang
 * @since 2023/3/10
 */
public class LimiterTestDemo {
    public static void main(String[] args) {
        String resourceCode = "main";
        SimpleLimiter simpleLimiter = new SimpleLimiter(resourceCode, 5, 5, TimeUnit.SECONDS, new MemoryLimiterRepository());

        for (int i = 1; i < 10; ++i) {
            System.out.println("=== 1 start try " + i);
            try {
                simpleLimiter.requireOne(resourceCode);
            } catch (Exception e) {
                System.out.println(e.toString());
            }
            System.out.println("=== 1 end   try " + i);
        }

        try {
            Thread.sleep(10 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        for (int i = 1; i < 10; ++i) {
            System.out.println("=== 1 start try " + i);
            try {
                simpleLimiter.requireOne(resourceCode);
            } catch (Exception e) {
                System.out.println(e.toString());
            }
            if (i == 2) {
                try {
                    System.out.println("sleep 3 second");
                    Thread.sleep(3 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("=== 1 end   try " + i);
        }

    }

}
